package com.framework.web.bus.controller;


import com.framework.core.domain.AjaxJson;
import com.framework.core.easyui.domain.DataGridModel;
import com.framework.core.easyui.util.PageUtil;
import com.framework.core.util.MyBeanUtils;
import com.framework.web.bus.domain.CookingSteps;
import com.framework.web.bus.service.CookbookService;
import com.framework.web.bus.service.CookingStepsService;
import com.framework.web.common.domain.BusinessConstants;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
* @Title: Controller
* @Description: 控制层
* @author zqtao
* @date 2016-09-08
* @version V1.0
*
*/
@Controller
@RequestMapping("/cookingSteps")
public class CookingStepsController {

private Logger logger = LoggerFactory.getLogger(this.getClass());


/**
* Spring注入资源信息模块service
*/
@Autowired
private CookingStepsService cookingStepsService;

@Resource
private CookbookService cookbookService;


/**
*
* 分页
* @param dgm
* @param cookingSteps
* @return
* @throws Exception
*/
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> list(DataGridModel dgm, CookingSteps cookingSteps, HttpServletRequest request) throws Exception {
    PageRequest pr = DataGridModel.getPageRequest(dgm);
    return PageUtil.getPageMap(cookingStepsService.getPageList(cookingSteps, pr));
}

/**
*  请求新增或修改页面
*
* @return
* @throws Exception
*/
@RequestMapping(value = "/toSaveAndUpdate/{cookbookId}/{id}", method = RequestMethod.GET)
public ModelAndView toSaveAndUpdate(@PathVariable("cookbookId") String cookbookId,@PathVariable("id") String id)
throws Exception {
    ModelAndView modelAndView = new ModelAndView("/bus/cookingSteps/cookingStepsForm");
    CookingSteps cookingSteps = new CookingSteps();
    String[] StringArr = null;
    if (StringUtils.isNotEmpty(id) && !"0".equals(id)) {
        cookingSteps = cookingStepsService.findById(id);
    } else {

    }
    cookingSteps.setCookbookId(cookbookId);
    if(cookingSteps.getImg()!=null){
         StringArr = cookingSteps.getImg().split(",");
        modelAndView.addObject("StringArr", StringArr);
    }
    modelAndView.addObject("cookingSteps", cookingSteps);
    return modelAndView;
}


/**
* 保存信息
*
* @param cookingSteps  信息实体
* @return
* @throws Exception
*/
@RequestMapping(value = "/saveAndUpdate", method = RequestMethod.POST)
@ResponseBody
public AjaxJson saveAndUpdate(HttpServletRequest request, HttpServletResponse response, CookingSteps cookingSteps) throws Exception {
    AjaxJson j = new AjaxJson();
    if (StringUtils.isNotEmpty(cookingSteps.getId())) {//更新
        CookingSteps r = cookingStepsService.findById(cookingSteps.getId());
        MyBeanUtils.copyBeanNotNull2Bean(cookingSteps, r);
        cookingSteps = r;
    }else {

    }
    /*初始化参数*/
    cookingSteps.setStatus(BusinessConstants.status.STATUS_NORMAL);
    cookingStepsService.save(cookingSteps);
    j.setMsg("保存成功");
    return j;
}

/**
* 根据id字符串删除数据信息
*
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value ="/delete", method = RequestMethod.POST)
@ResponseBody
public AjaxJson delete(String id) throws Exception {
    AjaxJson j = new AjaxJson();
    cookingStepsService.delete(id);
    j.setMsg("删除成功");
    return j;
}

/**
 * 跳转到Datagrid
 * @param request
 * @param response
 * @param cookbookId
 * @return
 */
@RequestMapping(value = "/toDatagrid/{cookbookId}",method = RequestMethod.GET)
@ResponseBody
public ModelAndView toDatagrid(HttpServletRequest request,HttpServletResponse response,
                               @PathVariable("cookbookId") String cookbookId){

    ModelAndView mav = new ModelAndView("/bus/cookingSteps/cookingStepsList");
    mav.addObject("cookbookId",cookbookId);
    return mav;
}

}